﻿using System.Drawing;
using System.IO;

using Elifart.Library.Serialization;

namespace Elifart.Library
{
    public class AthleteManager
    {
        #region Constructor

        private AthleteManager()
        {
        }

        #endregion

        #region Singleton

        private static AthleteManager _instance;

        public static AthleteManager GetManager()
        {
            if (_instance == null)
                _instance = new AthleteManager();
            return _instance;
        }

        #endregion

        #region Attributes and Properties

        private AthleteCollection _athletes;
        public AthleteCollection Athletes
        {
            get
            {
                if (this._athletes == null)
                    this._athletes = new AthleteCollection();
                return _athletes;
            }
        }

        #endregion

        #region Constants

        public const string ATHLETES_DIRECTORY = @"..\Elifart\Athletes";
        public const string ATHLETES_FILE = "Athletes.elf";

        public const string ATHLETES_BACKUP_DIRECTORY = @"..\Elifart\Backup";
        public const string ATHLETES_BACKUP_FILE = "Athletes_backup.elf";

        public const string ATHLETES_PICTURES_DIRECTORY = @"..\Elifart\Athletes\Pictures";

        #endregion

        #region Public Methods

        public bool CreateAthletesBackup()
        {
            try
            {
                if (!File.Exists(Path.Combine(ATHLETES_DIRECTORY, ATHLETES_FILE))) return true;

                if (!Directory.Exists(ATHLETES_BACKUP_DIRECTORY))
                    Directory.CreateDirectory(ATHLETES_BACKUP_DIRECTORY);

                File.Copy(Path.Combine(ATHLETES_DIRECTORY, ATHLETES_FILE), Path.Combine(ATHLETES_BACKUP_DIRECTORY, ATHLETES_BACKUP_FILE), true);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public Image GetAthletePicture(string imageName)
        {
            string flagFile = Path.Combine(ATHLETES_PICTURES_DIRECTORY, imageName);
            if (File.Exists(flagFile))
            {
                FileStream stream = new FileStream(flagFile, FileMode.Open);
                Bitmap bitmap = new Bitmap(stream);
                stream.Flush();
                stream.Close();
                return bitmap;
            }
            return null;
        }

        public bool LoadAthletes()
        {
            if (!File.Exists(Path.Combine(ATHLETES_DIRECTORY, ATHLETES_FILE)))
                return true;

            try
            {
                AthleteCollection athleteCollection =
                    XamlReader.Load<AthleteCollection>(Path.Combine(ATHLETES_DIRECTORY, ATHLETES_FILE));
                this._athletes = athleteCollection;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool LoadAthletesBackup()
        {
            if (!File.Exists(Path.Combine(ATHLETES_BACKUP_DIRECTORY, ATHLETES_BACKUP_FILE)))
                return true;

            try
            {
                AthleteCollection teamPlayerCollection =
                    XamlReader.Load<AthleteCollection>(Path.Combine(ATHLETES_BACKUP_DIRECTORY, ATHLETES_BACKUP_FILE));
                this._athletes = teamPlayerCollection;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void SaveAthletes()
        {
            if (!Directory.Exists(ATHLETES_DIRECTORY))
                Directory.CreateDirectory(ATHLETES_DIRECTORY);
            if (!Directory.Exists(ATHLETES_PICTURES_DIRECTORY))
                Directory.CreateDirectory(ATHLETES_PICTURES_DIRECTORY);

            XamlWriter.Save(this.Athletes, Path.Combine(ATHLETES_DIRECTORY, ATHLETES_FILE));
        }

        #endregion
    }
}
